import {Component, OnDestroy, OnInit} from '@angular/core';
import {CreateGameReq, GameApi, JoinGameReq} from "../../../api/GameApi";
import {ActivatedRoute, Router} from "@angular/router";
@Component({
  selector: 'app-game-house',
  templateUrl: './game-house.component.html',
  styleUrls: ['./game-house.component.scss']
})
export class GameHouseComponent implements OnInit, OnDestroy {

  timer: NodeJS.Timer | undefined
  players: Array<String> = []
  houseCode: number | undefined
  route: ActivatedRoute
  router: Router
  isHouseMaster: boolean = false

  constructor(route: ActivatedRoute, router: Router) {
    this.route = route;
    this.router = router;
    this.houseCode = this.route.snapshot.params['houseCode']


  }

  async syncPlayersInHouse() {
    const respResult = await GameApi.GetPlayerInHouse();
    if (respResult.success()) {
      this.players = respResult.data?.players!
    } else {
      respResult.resolveError(this.router, this.route)
    }

  }

  startGame() {

  }

  async ngOnInit() {
    this.timer = setInterval(() => {
      this.syncPlayersInHouse()
    }, 1000)
    return
  }

  ngOnDestroy(): void {
    clearInterval(this.timer)
  }


}
